﻿using System.IO;
using System.Net;
using System.Threading;

namespace SearchRS
{
    public class ProjectWSearcher : SearchWithResultsPage
    {
        protected override INotifications NotificationsClient { get; set; }

        protected override string BaseURL
        {
            get { return "http://www.projectw.org"; }
        }

        protected override string SearchURL
        {
            get { return BaseURL + "/search.php"; }
        }

        protected override string LoginURL
        {
            get { return BaseURL + "/ucp.php?mode=login"; }
        }

        protected override string SiteName
        {
            get { return "ProjectW"; }
        }

        protected override string SidCookieParam
        {
            get
            {
                return "ym_pop_freq_expiration284442=Thu, 12 Feb 2019 20:37:34 UTC; ym_pop_freq284442=1; project_w_u=; project_w_k=; project_w_sid=" + Sid;

            }
        }

        //private string cookie;
        protected override string SearchCookieParam
        {
            get
            {
                //return "pw_u=738313; pw_k=; pw_sid=" + Sid;

                return "pwCookie_u=738313; pwCookie_k=; pwCookie_sid=" + Sid;
//                    "Cookie: ym_pop_freq_expiration284442=Fri, 13 Feb 2019 20:54:07 UTC; ym_pop_freq284442=1; project_w_u=738313; project_w_k=b7b355b26c0a3cd8; project_w_sid=" +
//                  Sid + "; project_w_track=tf%3A%2825%3A%28k6u2%3A1%3B%29%3B%29%3Bt%3A%28k6u2%3A2ibv04%3B%29%3B";
            }
            set
            {
                if (value != null)
                {
                    int pos = value.LastIndexOf("project_w_sid=");
                    if (pos > -1)
                        Sid = value.Substring(pos + "project_w_sid=".Length, 32);
                    isPost = false;
                    //cookie = value;
                }
            }
        }

        protected override string PageCookieParam
        {
            get
            {              
                //return
                //    "ym_pop_freq_expiration284442=Fri, 13 Feb 2009 20:54:07 UTC; ym_pop_freq284442=1; pw_u=738313; pw_k=b7b355b26c0a3cd8; pw_sid=" +
                //    Sid + "; pw_track=tf%3A%2825%3A%28k6u2%3A1%3B%29%3B%29%3Bt%3A%28k6u2%3A2ibv04%3B%29%3B";

                return
                    "ym_pop_freq_expiration284442=Fri, 13 Feb 2009 20:54:07 UTC; ym_pop_freq284442=1; pwCookie_u=738313; pwCookie_k=b7b355b26c0a3cd8; pwCookie_sid=" +
                    Sid + "; pw_track=tf%3A%2825%3A%28k6u2%3A1%3B%29%3B%29%3Bt%3A%28k6u2%3A2ibv04%3B%29%3B";
            }
        }

        protected override string ResultTitleStartString
        {
            get { return "<td class=\"row1\">"; }//"class=\"topictitle\">"; }
        }

        protected override string ResultTitleFinishString
        {
            get { return "</a></p>"; }
        }

        protected override string TitleStartPosString
        {
            get { return "class=\"topictitle\">"; }
        }

        private bool isPost;
        protected override bool IsPost
        {
            get { return isPost; }
        }

        protected override string PostOrGetParams(bool searchAppsAndGamesAndEbooksToo)
        {
            return "keywords=" + m_searchString.Trim().Replace(" ", "+") + "&terms=all&topic_icon=0&author=&sc=1&sf=titleonly&sk=t&sd=d&sr=topics&st=0&ch=300&t=0&submit=Search";                                        
        }

        protected override string Sid { get; set; }

        public ProjectWSearcher(INotifications notificationsClient, SearchRsParams searchRsParams) :
            base(searchRsParams)
        {
            NotificationsClient = notificationsClient;
            SetSid();
        }

        private new void SetSid()
        {
            isPost = true;
/*            const string SID_STR = "project_w_sid=";

            var req = NetworkServices.NetworkServices.SendPostRequest(LoginURL, "", "",
                                                                      "username=maverick2830&password=maverick&redirect=&login=Log+in", "");

            var resp = req.GetResponse();

            string cookieStr = resp.Headers.Get("Set-Cookie");
            int pos = cookieStr.LastIndexOf(SID_STR) + SID_STR.Length;
            int pos2 = cookieStr.IndexOf(";", pos);
            Sid = cookieStr.Substring(pos, pos2 - pos);
            resp.Close();

            cookieStr = "project_w_u=738313; project_w_k=; project_w_sid=" + Sid;

            var req2 = NetworkServices.NetworkServices.SendGetRequest(
                "http://projectw.org/search.php?keywords=frozen+river&terms=all&topic_icon=0&author=&sc=1&sf=titleonly&sk=t&sd=d&sr=topics&st=0&ch=300&t=0&submit=Search", 
                "", "Cookie: " + cookieStr, "http://projectw.org/search.php?sid=" + Sid);


            var resp2 = req2.GetResponse();
            StreamReader sr = new StreamReader(resp2.GetResponseStream());
            string s = sr.ReadToEnd();
            */
            base.SetSid();
        }

    }
}
